﻿namespace MapleRock.P2PEmulator.Test
{
  using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Drawing;
  using System.Linq;
  using System.Text;

  using Core;
  using Core.Events;

  public static class Utils
  {
    public static Network CreateNetwork(
      int dimensions, 
      double areaLatitude,
      double areaLongitude,
      int citiesNumber,
      int citySizeMin,
      int citySizeMax,
      int peersNumber,
      PeerBehaviour behaviour)
    {
      Debug.Assert(dimensions > 1, "dimensions > 1");
      Debug.Assert(areaLatitude > 0, "areaLatitude > 0");
      Debug.Assert(areaLongitude > 0, "areaLongitude > 0");
      Debug.Assert(citiesNumber > 0, "citiesNumber > 0");
      Debug.Assert(citySizeMin > 0, "citySizeMin > 0");
      Debug.Assert(citySizeMax > 0, "citySizeMax > 0");
      Debug.Assert(citySizeMin <= citySizeMax, "citySizeMin <= citySizeMax");
      Debug.Assert(peersNumber > 0, "peersNumber > 0");

      var inputData = new XmlInputData();

      inputData.Dimensions = dimensions;
      inputData.MaxPeerLinksAllowed = dimensions + 1;

      // generate info space bounds
      inputData.InfoSpaceBounds = string.Empty;
      for (var i = 0; i < dimensions; ++i)
      {
        inputData.InfoSpaceBounds += "0 10 ";
      }

      inputData.InfoSpaceBounds = inputData.InfoSpaceBounds.TrimEnd();

      // fill geography data
      inputData.Geography = new XmlGeographyData[1];
      inputData.Geography[0] = new XmlGeographyData();
      inputData.Geography[0].AreaLatitude = areaLatitude;
      inputData.Geography[0].AreaLongitude = areaLongitude;
      inputData.Geography[0].CitiesNumber = citiesNumber;
      inputData.Geography[0].CitySizeMax = citySizeMax;
      inputData.Geography[0].CitySizeMin = citySizeMin;

      inputData.PeersNumber = peersNumber;

      var network = new Network();
      network.PeersBehaviour = behaviour;
      network.Initialize(inputData);

      return network;
    }
  }
}
